// src/routes/index.ts
import { Router } from 'express';
import { ResponseWrapper } from '../framework/utils/ResponseWrapper';
import { config } from "../framework/config";
import { logger } from '../framework/config/logger'; // 导入日志配置

import userRouter from './user.route'
import newsRouter from './news.route'

export const apiRouter = Router();

// 挂载各个模块的路由
apiRouter.use('/users', userRouter);
apiRouter.use('/news', newsRouter)

// 健康检查端点
apiRouter.get('/health', (req, res) => {
  // 将内存使用量转换为 MB（保留 2 位小数）
  const memoryUsage = process.memoryUsage();
  const memoryUsageMB = {
    rss: (memoryUsage.rss / 1024 / 1024).toFixed(2) + ' MB',              // 进程常驻内存
    heapTotal: (memoryUsage.heapTotal / 1024 / 1024).toFixed(2) + ' MB',  // 	V8 堆分配总量
    heapUsed: (memoryUsage.heapUsed / 1024 / 1024).toFixed(2) + ' MB',    // 	V8 堆使用量
    external: (memoryUsage.external / 1024 / 1024).toFixed(2) + ' MB',    // C++ 对象内存
  };

  ResponseWrapper.success(res, { 
    status: 'OK', 
    timestamp: new Date().toISOString(),
    uptime: `${process.uptime().toFixed(2)} seconds`,
    memory: memoryUsageMB,  // 替换为转换后的值
  }, `服务运行正常 | 环境: ${config.env}, 端口: ${config.port}`);

  logger.info(`健康检查触发 | 环境: ${config.env}, 内存: ${memoryUsageMB.heapUsed}`);
});

// 版本信息端点
apiRouter.get('/version', (req, res) => {
  ResponseWrapper.success(res, {
    version: '1.0.0',
    name: 'My TypeScript API',
    environment: process.env.NODE_ENV || 'development'
  }, '版本信息获取成功');
});